package server;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.OutputStream;

/**
 * 封装response对象  依赖于outputStream
 * 该对象需要提供核心方法 输入html
 *
 */
public class Response {

    private OutputStream outputStream;

    public Response() {
    }

    public Response(OutputStream outputStream) {
        this.outputStream = outputStream;
    }

    public void outputHtml(String url) throws IOException {
        //获取静态资源绝对路径
        String absoluteResourcePath=StaticResourceUtil.getAbsolutePath(url);
        //静态资源文件
        File file=new File(absoluteResourcePath);
        //判断静态资源文件是否存在
        if (file.exists() && file.isFile()){
            //读取静态资源文件,输出静态资源
            StaticResourceUtil.outputStaticResource(new FileInputStream(file), outputStream);
        }else {//输出404页面
            output(HttpProtocolUtil.getHttpHeader404());

        }
    }

    /**
     *  封装输出方法
     * @param content 内容
     */
    public void output (String content) throws IOException {
        outputStream.write(content.getBytes());
    }
}
